pftransloader.onFirefoxLoad = function(event) {
  document.getElementById("contentAreaContextMenu")
          .addEventListener("popupshowing", function (e){ pftransloader.showFirefoxContextMenu(e); }, false);
	

  // add history menu to panel menu
  var panelUiHistoryElement = document.getElementById("PanelUI-history");
  var historyVboxElement = panelUiHistoryElement.getElementsByClassName("panel-subview-body")[0];
  
  var historySeparatorApp = document.createElement("menuseparator");
  historySeparatorApp.setAttribute("id","pftransloader_historyMenuSeparatorApp");
  historyVboxElement.appendChild(historySeparatorApp);
  // add toolbarbutton
  var toolbarButtonElement = document.createElement("toolbarbutton");
  toolbarButtonElement.setAttribute("id", "pftransloader_historyMenuApp");
  toolbarButtonElement.setAttribute("type", "menu");
  toolbarButtonElement.setAttribute("label", pftransloader.strings.getString("historyLabel"));
  toolbarButtonElement.setAttribute("acceskey", pftransloader.strings.getString("historyAccesskey"));
  historyVboxElement.appendChild(toolbarButtonElement);
  pftransloader.addMenupopup(toolbarButtonElement, "App");
  
  // add history menu to old menu
  var goPopupElement = document.getElementById("goPopup");
  var historySeparatorHistory = document.createElement("menuseparator");
  historySeparatorHistory.setAttribute("id","pftransloader_historyMenuSeparatorHistory");
  goPopupElement.appendChild(historySeparatorHistory);
  var menuElement = document.createElement("menu");
  menuElement.setAttribute("id","pftransloader_historyMenuHistory");
  menuElement.setAttribute("label", pftransloader.strings.getString("historyLabel"));
  menuElement.setAttribute("acceskey", pftransloader.strings.getString("historyAccesskey"));
  goPopupElement.insertBefore(menuElement, historySeparatorHistory);  
  pftransloader.addMenupopup(menuElement, "History");
};

pftransloader.showFirefoxContextMenu = function(event) {
  // show or hide the menuitem based on what the context menu is on
  document.getElementById("pftransloader-contextsplitmenu").hidden = !gContextMenu.onImage;
  // save the image url here, because splitmenu's oncommand has gContextMenu == null
  if (gContextMenu.onImage) pftransloader.setImageUrl(gContextMenu.mediaURL);
};

pftransloader.addMenupopup = function(element, menu) {
  // add menupopup
  var menupopupElement = document.createElement("menupopup");
  menupopupElement.setAttribute("id","pftransloader_historyMenuList" + menu);
  element.appendChild(menupopupElement);
  
  // add empty item element
  var emptyListElement = document.createElement("menuitem");
  emptyListElement.className = "pftransloader_historyitem";
  emptyListElement.setAttribute("label", pftransloader.strings.getString("historyListEmpty"));
  var emptylist = menupopupElement.appendChild(emptyListElement);
  emptylist.disabled = true;
  
  // add menuseparator
  var menuseparatorElement = document.createElement("menuseparator");
  menuseparatorElement.setAttribute("id", "pftransloader_historySeparator" + menu);
  menupopupElement.appendChild(menuseparatorElement);
  
  // add full history and clear elements
  var fullHistoryElement = document.createElement("menuitem");
  fullHistoryElement.setAttribute("id", "pftransloader_showAllHistory" + menu);
  fullHistoryElement.addEventListener('command', function() { pftransloader.onShowAll()}, false);
  fullHistoryElement.setAttribute("label", pftransloader.strings.getString("historyShowAllLabel"));
  fullHistoryElement.setAttribute("acceskey", pftransloader.strings.getString("historyShowAllAccesskey"));
  menupopupElement.appendChild(fullHistoryElement);
  var clearElement = document.createElement("menuitem");
  clearElement.setAttribute("id", "pftransloader_historyClear" + menu);
  clearElement.addEventListener('command', function() { pftransloader.onClearHistory() }, false);
  clearElement.setAttribute("label", pftransloader.strings.getString("historyClearLabel"));
  clearElement.setAttribute("acceskey", pftransloader.strings.getString("historyClearAccesskey"));
  menupopupElement.appendChild(clearElement);
};

window.addEventListener("load", pftransloader.onFirefoxLoad, false);
